<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/9
     * Time: 上午10:33
     */

    namespace app\store\model;


    use think\Db;
    use think\Model;

    class AddressModel extends Model
    {

        protected $name = 'store_address';

        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i', 'update_time' => 'timestamp:Y-m-d H:i'];

        /**
         * 获取地址
         * @param $data_list
         * @param string $field
         * @return array
         */
        static public function GetIDs($data_list, $field = 'address_id')
        {

            $member = [];
            foreach ($data_list as $item) {
                if (isset($item[$field])) {
                    $member[$item[$field]] = $item[$field];
                }

            }
            return self::whereIn('id', $member)->column('id,address_detail');
        }


        /**
         * 获取地区
         * @param $value
         * @return array
         */
        static public function getOrigin(string $value)
        {
            return Db::name('store_address_origin')->whereIn('id', $value)->column('id,name');
        }

        static public function getOriginByAddressId(string $value)
        {
            $address = Db::name('store_address')->whereIn('id', $value)->find();
            return self::getOrigin($address['address_area']);
        }
        static public function getCompleteAddress($value){
            $address =  Db::name('store_address')->whereIn('id', $value)->find();

            $address_area = Db::name('store_address_origin')->whereIn('id', $address['address_area'])->column('id,name');
        
            return json_encode(['addr'=>join('-',$address_area) .'-'. $address['address_detail'],'name'=>$address['name'],'mobile'=>$address['mobile']]);
        }
    }